package com.axun.qqcountdemo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;

/**
 * Created by hz-java on 2018/9/29.
 */

public class MuAniUtils {

    public static void hideWithAlpha(final View view, long duration){
        ObjectAnimator animator = ObjectAnimator.ofFloat(view,"alpha",1,0);
        animator.setDuration(duration);
        animator.setInterpolator(new LinearInterpolator());
        animator.start();
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                view.setVisibility(View.GONE);
            }
        });
    }


    public static void showWithAlpha(final View view, long duration){
        view.setVisibility(View.VISIBLE);
        ObjectAnimator animator = ObjectAnimator.ofFloat(view,"alpha",0,1);
        animator.setDuration(duration);
        animator.setInterpolator(new LinearInterpolator());
        animator.start();
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);

            }
        });
    }

    public static void rotateView(View view,long duration){
        ObjectAnimator animator = ObjectAnimator.ofFloat(view,"rotation",0,90);
        animator.setDuration(duration);
        animator.setInterpolator(new LinearInterpolator());
        animator.start();
    }

    public static void resetRotateView(View view,long duration){
        ObjectAnimator animator = ObjectAnimator.ofFloat(view,"rotation",90,0);
        animator.setDuration(duration);
        animator.setInterpolator(new LinearInterpolator());
        animator.start();
    }


    public static void showWithWidth(final View view, long duration){
        view.setVisibility(View.VISIBLE);
        final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();

        ValueAnimator animator = ValueAnimator.ofInt(0,params.width);

        animator.setDuration(duration);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int width = (int) animation.getAnimatedValue();
                params.width = width;
                view.setLayoutParams(params);
            }
        });

        animator.start();
    }

    public static void hideWithWidth(final View view, long duration){

        final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
        final int viewWidth = params.width;

        ValueAnimator animator = ValueAnimator.ofInt(viewWidth,0);

        animator.setDuration(duration);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int width = (int) animation.getAnimatedValue();
                params.width = width;
                view.setLayoutParams(params);
            }

        });
        animator.start();
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                view.setVisibility(View.GONE);
                params.width = viewWidth;
                view.setLayoutParams(params);
            }
        });
    }


}
